/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sigeris;

import smile.Network;
import smile.SMILEException;

/**
 *
 * @author lenin
 */
public class SigerisNet {
    private String dir;
    private String file;
    private String tmp;
    private Network net;
    @SuppressWarnings("OverridableMethodCallInConstructor")
    public SigerisNet(String f){
        file=f;
        tmp=file;
        net = new Network();
        loadNetFile(tmp);
    }
    public void loadNetFile(String url){
        try {
            file=url;
		net.readFile(url); 
		net.updateBeliefs(); 
	}
	catch (SMILEException e) {
		System.out.println(e.getMessage());    
	}
    }
    public void setNetEvidence(String node, String value){
        try{
        net.updateBeliefs();
	net.getNode(node);
        if(net.isEvidence(node))net.clearEvidence(node);
        net.setEvidence(node, value);
        net.updateBeliefs();
        }
         catch (SMILEException e) {
             System.out.println("ERROR "+node+" "+value);
             System.out.println(e.getMessage());
        }
        
    }
    public double getValueNode(String node, String query){
        // 
        String[] aSuccessOutcomeIds;
        aSuccessOutcomeIds = net.getOutcomeIds(node);
        int outcomeIndex;
	for (outcomeIndex = 0; outcomeIndex < aSuccessOutcomeIds.length; outcomeIndex++)
            if (query.equals(aSuccessOutcomeIds[outcomeIndex]))
                break;
        // Getting the value of the probability:
        double[] aValues = net.getNodeValue(node);
	double P_SuccIsSuccGivenForeIsPoor = aValues[outcomeIndex];
        return P_SuccIsSuccGivenForeIsPoor;
    }
    public void clearAllEvidence(){
        net.clearAllEvidence();
    }
    public String getEvidences(){
        String [] ev=net.getAllNodeIds();
        String txtEv="<h2>Evidencias</h2><ul>";
        for(String node:ev){
            if(net.isEvidence(node))
                txtEv=txtEv+"<li>"+node+" "+net.getEvidence(node) +"</li>";
        }
        return txtEv+"</ul>";
    }
}
